﻿using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IServices.Plans;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ProTrack.Services.Plans
{
    /// <summary>
    /// 排班计划业务逻辑层
    /// </summary>
    public class PlanTeamServices : IPlanTeamServices
    {
        public PlanTeamServices(IBaseRepository db)
        {
            DB = db;
        }
        private IBaseRepository DB { get; }
        public async Task<bool> AddAsync(PlanTeam model, List<PlanTeamItem> list)
        {
            var result = await DB.InsertIdentityTranAsync(model, list);
            return result;
        }

        public async Task<bool> DeleteAsync(int[] Ids)
        {
            string sql = "delete PlanTeam where Id in @Ids";
            string sql2 = "delete PlanTeamItem where PlanTeamId = @Id";
            var result = await DB.DeleteAsync(sql, new { Ids });
            var result2 = await DB.DeleteAsync(sql2, new { Ids });
            return result;
        }

        public async Task<bool> DeleteAsync(int Id)
        {
            string sql = "delete PlanTeam where Id = @Id";
            string sql2 = "delete PlanTeamItem where PlanTeamId = @Id";
            var result = await DB.DeleteAsync(sql, new { Id });
            var result2 = await DB.DeleteAsync(sql2, new { Id });
            return result;
        }

        public async Task<IEnumerable<PlanTeam>> GetAllAsync()
        {
            string sql = "select * from PlanTeam";
            var list = await DB.QueryAllAsync<PlanTeam>(sql);
            return list;
        }

        public async Task<IEnumerable<PlanTeam>> GetAsync(int PageIndex, int PageSize, string TeamType, string PlanCode, string PlanName)
        {
            string sql = "select * from PlanTeam where 1=1";
            if (!string.IsNullOrEmpty(TeamType))
            {
                sql += " and TeamType like @TeamType";
            }
            if (!string.IsNullOrEmpty(PlanCode))
            {
                sql += " and PlanCode like @PlanCode";
            }
            if (!string.IsNullOrEmpty(PlanName))
            {
                sql += " and PlanName like @PlanName";
            }
            sql += " order by CreateTime desc offset @PageSize*(@PageIndex-1) row fetch next @PageSize rows only";
            var list = await DB.QueryAsync<PlanTeam>(sql, new { PageIndex, PageSize, TeamType = $"%{TeamType}%", PlanCode = $"%{PlanCode}%", PlanName = $"%{PlanName}%" });
            return list;
        }

        public async Task<int> GetCountAsync(string TeamType, string PlanCode, string PlanName)
        {
            string sql = "select count(*) from PlanTeam where 1=1";
            if (!string.IsNullOrEmpty(TeamType))
            {
                sql += " and TeamType like @TeamType";
            }
            if (!string.IsNullOrEmpty(PlanCode))
            {
                sql += " and PlanCode like @PlanCode";
            }
            if (!string.IsNullOrEmpty(PlanName))
            {
                sql += " and PlanName like @PlanName";
            }
            var count = await DB.GetRecoreCountAsync(sql, new { TeamType = $"%{TeamType}%", PlanCode = $"%{PlanCode}%", PlanName = $"%{PlanName}%" });
            return count;
        }
        public async Task<IEnumerable<PlanTeamItem>> GetPlanTeambyIdAsync(string PlanTeamId)
        {
            var list = await DB.QueryAsync<PlanTeamItem>("select  * from PlanTeamItem where PlanTeamId = @PlanTeamId", new { PlanTeamId });
            return list;
        }
        public async Task<bool> UpdateAsync(PlanTeam model, List<PlanTeamItem> list)
        {
            var result = await DB.UpdateIdentityTranAsync(model, list);
            return result;
        }
    }
}
